package com.dfec.book.util;


/**
 * 返回结果
 *
 * @author tangrg
 * @email 1446232546@qq.com
 * @date 2025-06-19 14:44:06
 */
public class AjaxResult<T> {

    private static final long serialVersionUID = 1L;

    /**
     * 状态码.
     */
    private int code;

    /**
     * 状态消息.
     */
    private String msg;

    /**
     * 数据.
     */
    private T data;

    public AjaxResult() {
        code = 0;
        msg = "success";
    }

    public static AjaxResult error() {
        return error(500, "未知异常，请联系管理员");
    }

    public static AjaxResult error(String msg) {
        return error(500, msg);
    }

    public static AjaxResult error(int code, String msg) {
        AjaxResult r = new AjaxResult();
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }

    public static AjaxResult error(String code, String msg) {
        AjaxResult r = new AjaxResult();
        r.setCode(Integer.parseInt(code));
        r.setMsg(msg);
        return r;
    }


    public static AjaxResult ok(String msg) {
        AjaxResult r = new AjaxResult();
        r.setMsg(msg);
        return r;
    }

    public static <T> AjaxResult ok(T data) {
        AjaxResult r = new AjaxResult<T>();
        r.setData(data);
        return r;
    }

    public static AjaxResult ok() {
        return new AjaxResult();
    }


    /**
     * 新增默认
     *
     * @param data
     * @return
     */
    public AjaxResult put(T data) {
        this.data = data;
        return this;
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
